/*
 * @Author: yangrongxin
 * @Date: 2021-07-14 10:57:53
 * @LastEditors: guoju
 * @LastEditTime: 2022-01-07 11:28:00
 */
import { useState, useEffect } from 'react';
import Taro, { useDidShow, useDidHide } from '@tarojs/taro';
import { View, Text, ScrollView } from '@tarojs/components';
import { NavBarSearch, OrderConfirmPanel } from '@/components/index';
import { Request } from '@/restful/index';
import ModalWapper from '@/components/CustomModal/modalWapper';
import { UserInfoCard, RecommandTest } from './components';
import { getHomeData } from './service';
import styles from './index_new.module.less';

const Home = () => {
  const [isOpened, setIsOpened] = useState<boolean>(false);
  const [orderId, setOrderId] = useState<string>('');
  const [refresherTrigger, setRefresherTrigger] = useState<boolean>(false);

  const [homeData, setHomeData] = useState({
    measureTableList: [],
  });

  const fetchHomeData = () => {
    getHomeData().then((res: any = {}) => {
      setHomeData(res);
    });
    setTimeout(() => {
      setRefresherTrigger(false);
    }, 500);
  };

  useEffect(() => {
    fetchHomeData();
  }, []);

  return (
    <View className="hxtPublicWapper">
      <NavBarSearch />
      <ScrollView
        scrollY
        scrollWithAnimation
        className={styles.container}
        refresherEnabled
        onRefresherRefresh={() => {
          setRefresherTrigger(true);
          fetchHomeData();
        }}
        refresherTriggered={refresherTrigger}
      >
        {/* 展示大当前用户的卡片 */}
        <UserInfoCard {...homeData} />
        {/* 推荐测评页面 */}
        <RecommandTest
          measureTableList={homeData.measureTableList}
          onChange={(id) => {
            setOrderId(id);
            setIsOpened(true);
          }}
        />
        <ModalWapper
          isOpened={isOpened}
          onClose={() => setIsOpened(false)}
          ContentChildren={<OrderConfirmPanel orderId={orderId} onClose={() => setIsOpened(false)} />}
          btnGroup={null}
          canClose
        />
      </ScrollView>
    </View>
  );
};

export default Home;
